// VBConversions Note: VB project level imports
using System.Diagnostics;
using System;
using System.Xml.Linq;
using System.Windows.Forms;
using System.Collections;
using System.Drawing;
using Microsoft.VisualBasic;
using System.Data;
using System.Collections.Generic;
using Microsoft.VisualBasic.Compatibility;
using System.Linq;
// End of VB project level imports


namespace 喷码数据生成工具
{
	public partial class frmSearchData
	{
		public frmSearchData()
		{
			InitializeComponent();
		}
		bool SearchClick = false;
		CommFunction.SearchData CurrentSearchInfo;
		public void OrderNo_TextChanged(System.Object sender, System.EventArgs e)
		{
			Btn_Search_Click(sender, e);
		}
		
		public void SearchData_FormClosing(object sender, System.Windows.Forms.FormClosingEventArgs e)
		{
			e.Cancel = true;
			if (SearchClick == false)
			{
				CommFunction.SearchResult = null;
			}
			CommFunction.CurrentSearchData = CurrentSearchInfo;
			CommFunction.SaveData.LastSearchTable = this.TableName.Text;
			e.Cancel = false;
		}
		
		public void SearchData_Load(object sender, System.EventArgs e)
		{
			//On Error Resume Next VBConversions Warning: On Error Resume Next not supported in C#
			this.TableName.Items.Clear();
			TableName.Items.Add("RemedyCode");
			if (CommFunction.SaveData.SearchTable != null) //加载保存的数据表
			{
				for (var i = 0; i <= CommFunction.SaveData.SearchTable.Count - 1; i++)
				{
					this.TableName.Items.Add(CommFunction.SaveData.SearchTable[System.Convert.ToInt32(i)].TableName);
				}
			}
			if (!string.IsNullOrEmpty(CommFunction.SaveData.LastSearchTable)) //加载上次访问的数据表
			{
				for (var i = 0; i <= TableName.Items.Count - 1; i++)
				{
					if (TableName.Items[System.Convert.ToInt32(i)].ToString().ToUpper() == CommFunction.SaveData.LastSearchTable.ToUpper())
					{
						TableName.SelectedIndex = System.Convert.ToInt32(i);
						break;
					}
				}
			}
			this.Show();
			if (this.TableName.Items.Count > 0) //加载数据结构表
			{
				Application.DoEvents();
				SearchData("Searching");
			}
			DataView.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing;
			DataView.ColumnHeadersHeight = 30;
			DataView.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
			CommFunction.ChangeButtonColor(Btn_Search);
		}
		
		private void TextBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
		{
			if (Strings.Asc(e.KeyChar) != 8)
			{
				if (!Information.IsNumeric(e.KeyChar))
				{
					e.Handled = true;
				}
			}
		}
		
		private void TextBox1_TextChanged(System.Object sender, System.EventArgs e)
		{
			
		}
		
		public void Btn_Search_Click(System.Object sender, System.EventArgs e)
		{
			SearchData(OrderNo.Text);
		}
		private void SearchData(string SearchValue)
		{
			try
			{
				string FiledStr = "";
				string SqlString = "";
				if (TableName.Text.ToUpper() == "RemedyCode".ToUpper())
				{
					FiledStr = "RemedycodeID,WorkBillNo,StartNo,EndNo,UniteNo,StepLength,Prefixion,Suffix,ProductNo,ProductName";
					SqlString = "select " + FiledStr + " from " + TableName.Text + " where workbillno like \'%" + SearchValue + "%\'";
				}
				else
				{
					SqlString = CurrentSearchInfo.SearchSql + "\'%" + SearchValue + "%\'";
				}
				DataSet ds = default(DataSet);
				string TmpStr = "";
				ds = CommFunction.Sql.Search(TableName.Text, SqlString);
				DataView.DataSource = ds.Tables[0].DefaultView;
				SearchClick = false;
			}
			catch (Exception)
			{
				//Me.Text = ex.Message
			}
		}
		private void SearchByData()
		{
			try
			{
				string FiledStr = "";
				string SqlString = "";
				if (TableName.Text.ToUpper() == "RemedyCode".ToUpper())
				{
					FiledStr = "RemedycodeID,WorkBillNo,StartNo,EndNo,UniteNo,StepLength,Prefixion,Suffix,ProductNo,ProductName";
					SqlString = "select " + FiledStr + " from " + TableName.Text + " where workbillno like \'%" + OrderNo.Text + "%\'";
				}
				else
				{
					SqlString = CurrentSearchInfo.SearchSql + "\'%" + OrderNo.Text + "%\'";
				}
				DataSet ds = default(DataSet);
				string TmpStr = "";
				ds = CommFunction.Sql.Search(TableName.Text, SqlString);
				DataView.DataSource = ds.Tables[0].DefaultView;
				SearchClick = false;
			}
			catch
			{
				
			}
		}
		private void ShowWorkBillHeaderText()
		{
			//On Error Resume Next VBConversions Warning: On Error Resume Next not supported in C#
			DataView.Columns["WorkBillID"].HeaderText = "编号";
			DataView.Columns["WorkBillNo"].HeaderText = "工单号";
			DataView.Columns["UniteQty"].HeaderText = "联数";
			DataView.Columns["ProductNo"].HeaderText = "产品编号";
			DataView.Columns["ProductName"].HeaderText = "产品名称";
			DataView.Columns["StartNo"].HeaderText = "起始号码";
			DataView.Columns["EndNo"].HeaderText = "终止号码";
			DataView.Columns["StepLength"].HeaderText = "步长值";
			DataView.Columns["CheckRules"].HeaderText = "校验方式";
		}
		private void ShowRemedyCodeHeaderText()
		{
			//On Error Resume Next VBConversions Warning: On Error Resume Next not supported in C#
			//"RemedycodeID,WorkBillNo,StartNo,EndNo,UniteNo,StepLength,Prefixion,Suffix,ProductNo,ProductName"
			DataView.Columns["RemedycodeID"].HeaderText = "编号";
			DataView.Columns["WorkBillNo"].HeaderText = "工单号";
			DataView.Columns["StartNo"].HeaderText = "起始号码";
			DataView.Columns["EndNo"].HeaderText = "终止号码";
			DataView.Columns["UniteNo"].HeaderText = "联次";
			DataView.Columns["StepLength"].HeaderText = "步长";
			DataView.Columns["Prefixion"].HeaderText = "前缀";
			DataView.Columns["Suffix"].HeaderText = "后缀";
			DataView.Columns["ProductNo"].HeaderText = "产品编号";
			DataView.Columns["ProductName"].HeaderText = "产品名称";
		}
		public void SetTableDescription(string mTableName)
		{
			
			string sqlStr1 = "SELECT   * FROM   ::fn_listextendedproperty (null, \'user\', \'dbo\', \'table\',\'" + mTableName + "\', \'column\', default)";
			DataSet tmpDs = default(DataSet);
			try
			{
				
				tmpDs = CommFunction.Sql.Search(mTableName, sqlStr1);
				int DC = System.Convert.ToInt32(tmpDs.Tables[0].Rows.Count - 1);
				string mField = Constants.vbNull;
				for (var i = 0; i <= DC; i++)
				{
					try
					{
						
						mField = System.Convert.ToString(tmpDs.Tables[0].Rows[i][1].ToString());
						DataView.Columns[mField].HeaderText = System.Convert.ToString(tmpDs.Tables[0].Rows[i][3].ToString());
					}
					catch
					{
					}
				}
			}
			catch
			{
			}
		}
		public void DataView_DataSourceChanged(object sender, System.EventArgs e)
		{
			if (TableName.Text.ToUpper() == "RemedyCode".ToUpper())
			{
				ShowRemedyCodeHeaderText();
			}
			else
			{
				SetTableDescription(System.Convert.ToString(((DataView) DataView.DataSource).Table.TableName));
			}
		}
		
		public void DataView_RowHeaderMouseDoubleClick(System.Object sender, System.Windows.Forms.DataGridViewCellMouseEventArgs e)
		{
			DataView DV = DataView.DataSource;
			string tName = DV.Table.TableName;
			DataGridViewRow CurRow = DataView.CurrentRow;
			LoadData(tName, CurRow);
			SearchClick = true;
		}
		public void LoadData(string sTableName, DataGridViewRow SelectRow)
		{
			try
			{
				string SqlStr = "";
				CommFunction.SearchResult = null;
				if (sTableName.ToUpper() == "RemedyCode".ToUpper())
				{
					//RemedycodeID,WorkBillNo,StartNo,EndNo,UniteNo,StepLength,Prefixion,Suffix,ProductNo,ProductName
					string Order = "";
					string UniteN = "";
					DataSet Rds = default(DataSet);
					Order = SelectRow.Cells["WorkBillNo"].Value.ToString();
					UniteN = SelectRow.Cells["UniteNo"].Value.ToString();
					SqlStr = "select WorkBillNo,StartNo,EndNo,UniteNo,StepLength,Prefixion,Suffix from RemedyCode where workbillno=\'" + Order + "\' and UniteNo=\'" + UniteN + "\'";
					Rds = CommFunction.Sql.Search("RemedyCode", SqlStr);
					if (Rds == null)
					{
						Interaction.MsgBox("查找错误，找不到数据！", (int) MsgBoxStyle.OkOnly + MsgBoxStyle.Information, "提示");
						return ;
					}
					else if (Rds.Tables[0].Rows.Count <= 0)
					{
						Interaction.MsgBox("查找错误，找不到数据！", (int) MsgBoxStyle.OkOnly + MsgBoxStyle.Information, "提示");
						return ;
					}
					else
					{
						CommFunction.SearchResult = Rds.Tables[0];
						SearchClick = true;
						this.Close();
					}
				}
				else
				{
					string[] SqlTJ = null;
					int a = 0;
					int b = 0;
					string TJ = "";
					string tmp = CurrentSearchInfo.DownLoadDataSql;
					string tjTmp = "";
					string tjValue = "";
					DataSet Rds = default(DataSet);
					a = tmp.LastIndexOf("[");
					b = tmp.LastIndexOf("]");
					if (b > a & a > 0) //查找条件，并构建条件语句
					{
						SqlStr = tmp.Substring(0, a);
						try
						{
							tjTmp = tmp.Substring(a + 1, b - a - 1);
						}
						catch (Exception)
						{
							tjTmp = "";
						}
						SqlTJ = tjTmp.Split(',');
						for (var i = 0; i <= SqlTJ.Length - 1; i++)
						{
							try
							{
								tjValue = SelectRow.Cells[SqlTJ[(int) i]].Value.ToString();
								if (TJ != "")
								{
									TJ = TJ + " and " + SqlTJ[(int) i].Trim() + " =\'" + tjValue + "\'";
								}
								else
								{
									TJ = SqlTJ[(int) i].Trim() + " =\'" + tjValue + "\'";
								}
							}
							catch
							{
							}
						}
						SqlStr = SqlStr + " " + TJ;
					}
					Rds = CommFunction.Sql.Search(sTableName, SqlStr);
					if (Rds == null)
					{
						Interaction.MsgBox("查找错误，找不到数据！", (int) MsgBoxStyle.OkOnly + MsgBoxStyle.Information, "提示");
						return ;
					}
					else if (Rds.Tables[0].Rows.Count <= 0)
					{
						Interaction.MsgBox("查找错误，找不到数据！", (int) MsgBoxStyle.OkOnly + MsgBoxStyle.Information, "提示");
						return ;
					}
					else
					{
						CommFunction.SearchResult = Rds.Tables[0];
						SearchClick = true;
						this.Close();
					}
				}
			}
			catch (Exception EX)
			{
				Interaction.MsgBox(EX.Message, MsgBoxStyle.Critical, "错误");
			}
		}
		
		public void DataView_CellContentClick(System.Object sender, System.Windows.Forms.DataGridViewCellEventArgs e)
		{
			
		}
		
		public void TableName_SelectedIndexChanged(System.Object sender, System.EventArgs e)
		{
			string tmpSelected = TableName.Items[TableName.SelectedIndex].ToString();
			CurrentSearchInfo = null;
			if (tmpSelected.ToUpper() != "RemedyCode".ToUpper())
			{
				if (CommFunction.SaveData.SearchTable != null)
				{
					for (var i = 0; i <= CommFunction.SaveData.SearchTable.Count - 1; i++)
					{
						if (tmpSelected.ToUpper() == CommFunction.SaveData.SearchTable[System.Convert.ToInt32(i)].TableName.ToUpper)
						{
							CurrentSearchInfo = CommFunction.SaveData.SearchTable[System.Convert.ToInt32(i)];
							break;
						}
					}
				}
			}
		}
	}
}
